// SPDX-License-Identifier: GPL-2.0-only
/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2023-2023. All rights reserved.
 * Author: Huawei OS Kernel Lab
 * Create: Thu Aug 15 16:14:23 2023
 */

#include <linux/export.h>
#include <linux/kernel.h>
#include <linux/namei.h>
#include <linux/fs.h>
#include <linux/file.h>
#include <linux/hash.h>
#include <linux/mount.h>

/* fs/exec.c */
bool path_noexec(const struct path *path)
{
	return ((unsigned long)path->mnt->mnt_flags & MNT_NOEXEC) ||
		(path->mnt->mnt_sb->s_iflags & SB_I_NOEXEC);
}

/* fs/nsfs.c */
const struct dentry_operations ns_dentry_operations = { NULL };

struct file *transfs_fget(unsigned int fd)
{
	return (struct file *)liblinux_pal_transfs_fget(fd);
}
EXPORT_SYMBOL(transfs_fget);

void transfs_fput(struct file *file)
{
	if (file == NULL)
		return;

	liblinux_pal_transfs_fput(file);
}
EXPORT_SYMBOL(transfs_fput);

/* fs/file.c */
int get_unused_fd_flags(unsigned int flags)
{
	return liblinux_pal_get_unused_fd_flags(flags);
}
EXPORT_SYMBOL(get_unused_fd_flags);

void fd_install(unsigned int fd, struct file *file)
{
	liblinux_pal_fd_install(fd, file);
}
EXPORT_SYMBOL(fd_install);

void put_unused_fd(unsigned int fd)
{
	liblinux_pal_put_unused_fd((int)fd);
}
EXPORT_SYMBOL(put_unused_fd);

static void __fget(void *file)
{
	get_file(file);
}

struct file *fget(unsigned int fd)
{
	struct file *filp = NULL;

	if (fd == UINT_MAX)
		return ERR_PTR(-EBADF);

	filp = liblinux_pal_fget_ex(ANON_FD, fd, __fget);
	if (filp == NULL)
		return ERR_PTR(-EBADF);

	return filp;
}
EXPORT_SYMBOL(fget);

unsigned long __fdget(unsigned int fd)
{
	struct file *filp = fget(fd);

	if (IS_ERR(filp))
		return 0;

	return FDPUT_FPUT | (unsigned long)filp;
}
EXPORT_SYMBOL(__fdget);
